<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template v-slot:condition="{row}">
      <input type="hidden" :name="row.name" :value="row.value">
      <span class="text-ellipsis" v-html="condition_info(JSON.parse(row.value))"></span>
      <forms v-show="!row.is_info" :config="row.config" remove_name="1" @change="onchange" style="width: 100%;" :btns="[]"></forms>
    </template>
  </forms>
</template>

<script>
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import {_config} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";
import {vue_template} from "@/js/dom_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let type_map = _config('status_alias_map.kickback_type.type');
    let type_tips = '不同佣金类型，在确认佣金的时机，和对应的分佣发起者是有区别的。并且此项无法更改！\n';
    type_tips += '【订单佣金】下单时添加佣金，订单成交后确认佣金；分佣发起者为下单的消费者\n';
    type_tips += '【邀新佣金】用户注册时添加并确定佣金；分佣发起者为注册用户\n';
    type_tips += '【邀请下单佣金】下单时添加佣金，订单成交后确认佣金；分佣发起者为邀请消费者下单的用户\n';
    type_tips += '【等级晋升奖励】用户等级晋升时添加并确定佣金，每个等级只有第一次晋升的时候有效；分佣发起者为升级的用户\n';
    type_tips += '【命名佣金】此类型佣金只是为命名而存在，不需要分配给租户，用于和其他机制关联添加佣金时区分，例如：积分兑换';
    let forms_config = {
      title: {label:'佣金名称',name:'title',type:'text_one',tips:''},
      type: {label:'类型',name:'type',type:'select_one',config:type_map, is_info:(id>0), tips:type_tips},
      condition: {label:'佣金条件',name:'condition',type:'condition',config:[],value:'{}',tips:''},
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
      function_map: [],
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/kickback_type/edit?id=`+this.id : '/admin/kickback_type/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('kickback_type/index', '$refs.list_url.list');
          iframe.hide('kickback_type/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    },
    onchange(config){
      let app = this;
      let func = config.function.value;
      let param = [];
      if (!app.function_map[func]) return;
      s94Mjs.each(app.function_map[func].param, function(v){
        let val = config[v]?.value;
        param.push(typeof(val)=='undefined'?'':val)
      })
      let data = {
        function: func,
        param: param,
      }
      app.config.condition.value = JSON.stringify(data);
    },
    condition_info(condition){
      if (!this.function_map[condition.function]) return '';
      let fuc = this.function_map[condition.function];
      let data = fuc.info_param || [];
      fuc.param.forEach(function(k,i){
        data[k] = condition.param[i];
      })
      return vue_template(fuc.info, data);
    },
    change_function(row, func){
      let app = this;
      s94Mjs.each(app.config.condition.config, function(v,k){
        if (k=='function') return;
        v.remove = !~app.function_map[func].param.indexOf(k);
      })
    },
    condition_init(){
      let app = this;
      if (app.config.condition.value!='{}' && Object.keys(app.config.condition.config).length>0){
        let condition = JSON.parse(app.config.condition.value);
        app.config.condition.config.function.value = condition.function;
        let param = app.function_map[condition.function]?.param || {};
        s94Mjs.each(param, function(k,i){
          app.config.condition.config[k].value = condition.param[i];
          app.config.condition.config[k].remove = false;
        })
      }
    },
  },
  watch: {
    'config.type.value'(v, oldV){
      this.config.condition.remove = (v==4);
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/kickback_type/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        app.condition_init();
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
    ajax({url:'/admin/kickback_type/function_config',cache:true},function (res){
      let function_list=[];
      s94Mjs.each(res.data.function_map, function(v,k){
        function_list.push({content:v.content, value:k});
      })
      let config = {
        function: {label:'条件主体',name:'',type:'select_one',config:function_list,tips:'',onchange: app.change_function},
      }
      s94Mjs.each(res.data.field_map, function(row,k){
        row.remove = true;
        config[k] = row;
      })
      app.config.condition.config = config;
      app.function_map = res.data.function_map;
      app.condition_init();
    })
  }
}
</script>
